try{
	
var sounds = [];
var pfdPrefGround = true;
var soundsPref = true;

var lastSound = 0;
var lastAltSound = 0;
var lastAlt = 0;

var altCallouts = false;
var altAlertAlt = 500;
var altAlert = false;

var gpwsCallouts = true;

var radio_gs = 0;

function playAltSnd(alt)
{
	if (gpwsCallouts && lastAltSound != alt)// && lastAltSound != 0)
	{
		playSnd("a"+alt);
	}
	lastAltSound = alt;
}

function playSnd(name)
{
	if (soundsPref && lastSound!=name){sounds[name].play();}
	lastSound = name;
}
function force1PlaySound(name)
{
	//does not play if sounds are off
	lastSound = "";
	playSnd(name);
}
function force2PlaySound(name)
{
	//disregards the sound toggle
	sounds[name].play();
	lastSound = name;
}
function loadSnd(name)
{
	sounds[name] = Titanium.Media.createSound('app://sounds/'+name+'.wav');
}
loadSnd("a10");
loadSnd("a20");
loadSnd("a30");
loadSnd("a40");
loadSnd("a50");
loadSnd("a100");
loadSnd("a200");
loadSnd("a300");
loadSnd("a400");
loadSnd("a500");
loadSnd("a1000");
loadSnd("alt");

setInterval("_betty()",500);
//setInterval("_coms()",1500);

$(function()
{
	$("#signal-form").dialog({
		closeOnEscape: true,
		autoOpen: false,
		width: 375,
		height: 500,
		modal: false,
		buttons: {
			Cancel: function() {
				$(this).dialog("close");
			}
		}
	});
	
	//PFD {
	$("#groundLevel").val(groundLevel);
	$("#setGL").click(function(){
		groundLevel = plane_alt;
	});
	$("#groundLevel").click(function(){pfdPrefGround=true;});
	$("#seaLevel").click(function(){pfdPrefGround=false;});
	$("#setGroundLevel").click(function(){groundLevel = parseInt($("#groundLevelInput").val());});
	$("#resetGroundLevel").click(function(){$("#groundLevelInput").val(groundLevel.toString());});
	$("#currentGroundLevel").click(function(){
		groundLevel = Math.floor(plane_alt);
		$("#groundLevelInput").val(groundLevel.toString());
	});
	//} PFD
	
	//SOUNDS {
	$("#testSounds").click(function(){
		force1PlaySound("alt");
	});
	$("#soundOn").click(function(){
		muteOff();
	});
	$("#soundOff").click(function(){
		mute();
	});
	$("#toggleSounds").click(function(){
		toggleMute();
	});
	
	$("#altCallouts").button().click(function(){
		gpwsCallouts=!gpwsCallouts;
		//alert(gpwsCallouts);
	});
	
	$("#terrainAlertAlt").change(function(){
		altAlertAlt = parseInt($(this).val());
	});
	$("#terrainAlertEn").button().click(function(){
		if ($(this).is(':checked')){altCalloutsOn();}
		else{altCalloutsOff();}
	});
	//} SOUNDS
});

function _betty()
{
	_betty_alt();
}

function _betty_alt()
{
	var alt = plane_alt - groundLevel;
	var called = false;
	
	for (var a = 1; a<=5; a++)
	{
		if ((alt>(a*10))^(lastAlt>(a*10)))//(within(alt,(a*10)-2,(a*10)+2))
		{playAltSnd(a*10);called=true;break;}
		if ((alt>(a*100))^(lastAlt>(a*100)))//(within(alt,(a*100)-2,(a*100)+2))
		{playAltSnd(a*100);called=true;break;}
	}
	if (!called&&((alt>1000)^(lastAlt>1000)))//(within(alt,1000,1009))
	{playAltSnd(1000);}
	if (altCallouts&&((alt>altAlertAlt)^(lastAlt>altAlertAlt)))
	{
		force1PlaySound("alt");
	}
	
	lastAlt = alt;
}

function _coms()
{
	$.ajax({
		type: "GET",
		url: "http://localhost:8080/radios",
		dataType: "xml",
		success: function(xml) {
			$(xml).find("gs").find("signal").each(function(){
				radio_gs = parseInt($(this).text());
			});
			//radio_gs = parseInt($(xml).find("gs").text());
			$("#wireless_strength_gs").progressbar( "value" , radio_gs );
			$(xml).find("p").find("signal").each(function(){
				radio_p = parseInt($(this).text());
			});
			//radio_p = parseInt($(xml).find("p").text());
			$("#wireless_strength_p").progressbar( "value" , radio_p );
		},
		error: function(xml) {
			$("#wireless_strength_gs").progressbar( "value" , 0 );
			$("#wireless_strength_p").progressbar( "value" , 0 );
			playSnd("link");
		}
	});
}

function toggleMute()
{
	//alert("DERP");
	if (soundsPref){mute();}else{muteOff();}
}
function mute()
{
	//alert("Mute");
	if (!soundsPref){return;}//already on mute
	soundsPref=false;
	if (!$("#soundOff").is(':checked')){$("#soundOff").click();}
	//$("#soundOff").checked = true;
	$("#toggleSounds").animate({color: "#FF0000" }, 500);
}
function muteOff()
{
	//alert("Un-Mute");
	if (soundsPref){return;}//already off mute
	soundsPref=true;
	if (!$("#soundOn").is(':checked')){$("#soundOn").click();}
	//$("#soundOn").checked = true;
	$("#toggleSounds").animate({color: "#FFFFFF" }, 500);
}

function altCalloutsOn()
{
	//alert("ALT CALLOUT ON");
	altCallouts = true;
}

function altCalloutsOff()
{
	//alert("ALT CALLOUT OFF");
	altCallouts = false;
}

function within(x,low,high)
{
	return (x>=low && x<=high);
}

}
catch(e)
{
	alert("Error in betty.js : "+e);
}
